package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.model.ZcPolicy;
import net.microfigure.service.IZcPolicyService;
import net.microfigure.commons.base.BaseController;

/**
 * <p>
 * 慢性病政策表 前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-15
 */
@Controller
@RequestMapping("/zcPolicy")
public class ZcPolicyController extends BaseController {

	@Autowired 
	private IZcPolicyService zcPolicyService;

	@GetMapping("/manager")
	public String manager() {
		return "admin/zcPolicy/zcPolicyList";
	}


	/**
	 * 分页
	 * @return
	 */
	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		zcPolicyService.selectDataGrid(pageInfo);
		return pageInfo;


	}

	/**
	 * 添加页面
	 * @return
	 */
	@GetMapping("/addPage")
	public String addPage() {
		return "admin/zcPolicy/zcPolicyAdd";
	}

	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public Object add(@Valid ZcPolicy zcPolicy) {

		System.out.println(zcPolicy.getMaxLine());
		boolean b = zcPolicyService.insert(zcPolicy);
		if (b) {
			return renderSuccess("添加成功！");
		} else {
			return renderError("添加失败！");
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		boolean b = zcPolicyService.deleteById(id);
		if (b) {
			return renderSuccess("删除成功！");
		} else {
			return renderError("删除失败！");
		}
	}

	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/editPage")
	public String editPage(Model model, Long id) {
		ZcPolicy zcPolicy = zcPolicyService.selectById(id);
		model.addAttribute("zcPolicy", zcPolicy);
		return "admin/zcPolicy/zcPolicyEdit";
	}

	/**
	 * 编辑
	 * @param 
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public Object edit(@Valid ZcPolicy zcPolicy) {
		boolean b = zcPolicyService.updateById(zcPolicy);
		if (b) {
			return renderSuccess("编辑成功！");
		} else {
			return renderError("编辑失败！");
		}
	}
}
